let baseUrl = process.env.NODE_ENV =='production' ? 'http://localhost:9999' : '';


function http(option){
  let {url,method='GET',data={}} = option;
  let obj= {
    method,
  }
  url = baseUrl + url;
  let str = '';
  Object.keys(data).forEach(item=>{// data{a:123,b:234}  a=123&b=234&
    str += `${item}=${data[item]}&`
  })
  str = str.replace(/&$/,'')
  if(/get/i.test(method)){
    url+='?'+str
  }else{
    Object.assign(obj,{
      body:str,
      headers:{
        'content-type': 'application/x-www-form-urlencoded'
      }
    })
  }
  return fetch(url,obj).then(data=>data.json())
}
// http({url:,method:,data})
http.get = function(url,data){

  return http({
    url,
    data,
    method:'get'
  })
}
http.post = function(url,data){
  return http({
    url,
    data,
    method:'post'
  })
}
export default http